package edu.study.user.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.api.common.enums.BaseResultCodeEnum;
import edu.study.user.api.dto.RoleAuthDto;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.req.roleauth.QueryRoleAuthReq;
import edu.study.user.api.req.roleauth.RoleAuthReq;
import edu.study.user.dao.mapperExt.RoleDOMapperExt;
import edu.study.user.dao.mapperExt.RoleAuthDOMapperExt;
import edu.study.user.dao.mapperExt.AuthDOMapperExt;
import edu.study.user.dbBean.RoleDO;
import edu.study.user.util.UserUtils;
import edu.study.user.dbBean.RoleAuthDO;
import edu.study.user.dbBean.AuthDO;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RoleAuthService {
	
	@Autowired
	private RoleAuthDOMapperExt roleAuthDOMapperExt;

	@Autowired
	private RoleDOMapperExt roleDOMapperExt;
	
	@Autowired
	private AuthDOMapperExt userDOMapperExt;
	
	@Autowired
	private TransactionTemplate transactionTemplate;

	public BaseRespDto addRoleAuth(RoleAuthReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if(roleDO == null) {
			log.info("RoleAuthService.addRoleAuth, 角色不存在! roleNo={}", req.getRoleNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<RoleAuthDO> roleAuthDOList = new ArrayList<RoleAuthDO>(req.getAuthNoList().size());
		for (String authNo : req.getAuthNoList()) {
			AuthDO userDO = userDOMapperExt.queryByAuthNo(authNo);
			if(userDO == null) {
				log.info("RoleAuthService.addRoleAuth, 用户不存在! authNo={}", authNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			RoleAuthDO newRoleAuthDO = new RoleAuthDO();
			newRoleAuthDO.setRoleNo(req.getRoleNo());
			newRoleAuthDO.setAuthNo(authNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newRoleAuthDO.setCreator(userDetailDto.getLoginName());
			}
			roleAuthDOList.add(newRoleAuthDO);
		}
		
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (RoleAuthDO roleAuthDO : roleAuthDOList) {
					roleAuthDOMapperExt.insertSelective(roleAuthDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto deleteRoleAuth(RoleAuthReq req) {
		BaseRespDto resp = new BaseRespDto();
		List<RoleAuthDto> roleAuthListAll = new ArrayList<RoleAuthDto>(req.getAuthNoList().size());
		for (String authNo : req.getAuthNoList()) {
			List<RoleAuthDto> roleAuthList = roleAuthDOMapperExt.queryRoleAuthList(req.getRoleNo(), authNo);
			if(CollectionUtils.isEmpty(roleAuthList)) {
				log.info("RoleAuthService.deleteRoleAuth, 角色用户不存在! roleNo={}, authNo={}", req.getRoleNo(), authNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			roleAuthListAll.addAll(roleAuthList);
		}
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (RoleAuthDto roleAuthDto : roleAuthListAll) {
					roleAuthDOMapperExt.deleteByPrimaryKey(roleAuthDto.getId());
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyRoleAuth(RoleAuthReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if(roleDO == null) {
			log.info("RoleAuthService.modifyRoleAuth, 角色不存在! roleNo={}", req.getRoleNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<RoleAuthDO> roleAuthDOListNew = new ArrayList<RoleAuthDO>(req.getAuthNoList().size());
		for (String authNo : req.getAuthNoList()) {
			AuthDO userDO = userDOMapperExt.queryByAuthNo(authNo);
			if(userDO == null) {
				log.info("RoleAuthService.modifyRoleAuth, 用户不存在! authNo={}", authNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			RoleAuthDO newRoleAuthDO = new RoleAuthDO();
			newRoleAuthDO.setRoleNo(req.getRoleNo());
			newRoleAuthDO.setAuthNo(authNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newRoleAuthDO.setCreator(userDetailDto.getLoginName());
			}
			roleAuthDOListNew.add(newRoleAuthDO);
		}
		List<RoleAuthDto> roleAuthListOld = roleAuthDOMapperExt.queryRoleAuthList(req.getRoleNo(), null);
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				if(!CollectionUtils.isEmpty(roleAuthListOld)) {
					for (RoleAuthDto roleAuthDto : roleAuthListOld) {
						roleAuthDOMapperExt.deleteByPrimaryKey(roleAuthDto.getId());
					}
				}
				for (RoleAuthDO roleAuthDO : roleAuthDOListNew) {
					roleAuthDOMapperExt.insertSelective(roleAuthDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public RespDto<List<RoleAuthDto>> queryRoleAuthList(QueryRoleAuthReq req) {
		RespDto<List<RoleAuthDto>> resp = new RespDto<List<RoleAuthDto>>();
		List<RoleAuthDto> roleAuthList = roleAuthDOMapperExt.queryRoleAuthList(req.getRoleNo(), req.getAuthNo());
		resp.setData(roleAuthList);
		return resp;
	}
}
